Annotate cell placed in plan or profile
The below code snippet shows annotating the cell placed in plan or profile model. Here in the code cell is annotated with the cell name and center of cell is used as placement location.
//Required References
using System;
using Bentley.DgnPlatformNET;
using Bentley.MstnPlatformNET;
using Bentley.CifNET.GeometryModel.SDK;
using Bentley.GeometryNET;
using System.Diagnostics;
using Bentley.Interop.MicroStationDGN;
public void AnnotateCell()
{
try
{
//Get active dgn model either plan or profile
DgnModel dgnModel = Session.Instance.GetActiveDgnModel();
if (dgnModel.IsPlanModel() || dgnModel.IsProfileModel())
{
//Get all graphical elements cache from active model reference
Bentley.Interop.MicroStationDGN.ElementCache elementCache = Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.ActiveModelReference.GraphicalElementCache;
//Scan to get all elements
ElementEnumerator elementEnumerator = elementCache.Scan();
//Add text annotation for first cell element
while (elementEnumerator.MoveNext())
{
//Get current element and check if it is of type cell
Bentley.Interop.MicroStationDGN.Element currentElement = elementEnumerator.Current;
if (currentElement.IsCellElement())
{
Bentley.Interop.MicroStationDGN.CellElement cellElement = (Bentley.Interop.MicroStationDGN.CellElement)currentElement;
//User might need unit conversion here on dOrigin based on unit settings
DPoint3d dOrigin = new DPoint3d(cellElement.Origin.X, cellElement.Origin.Y, cellElement.Origin.Z);
//Create new DgnTextStyle element for annotation definition
DgnFile activeDgnFile = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnFile();
DgnTextStyle textStyle = DgnTextStyle.GetByName("Cell Annotate", activeDgnFile);
if (null == textStyle)
{
textStyle = new DgnTextStyle("Cell Annotate", activeDgnFile);
textStyle.SetProperty(TextStyleProperty.Width, 400D);
textStyle.SetProperty(TextStyleProperty.Height, 400D);
textStyle.Add(activeDgnFile);
}
string textString = string.Format("Cell = " + cellElement.Name);
//New TextBlock for defining annotation
TextBlock textBlock = new TextBlock(textStyle, Session.Instance.GetActiveDgnModel());
textBlock.AppendText(textString);
textBlock.SetUserOrigin(dOrigin);
//Add text annotation
Bentley.DgnPlatformNET.Elements.TextElement textElement = (Bentley.DgnPlatformNET.Elements.TextElement)Bentley.DgnPlatformNET.Elements.TextElement.CreateElement(null, textBlock);
textElement.AddToModel();
break;
}
}
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
Output